close

class Something {
final int x;


Something() {
this(10);
System.out.println(x);
}


Something(int x) {
x = 7;
this.x = x;

}


}

public class myThis {
public static void main(String[] args)
{

new Something();   

//Something hi = new Something();
//System.out.println(hi.x);    

//上兩行效果同new Something();


}

}

 

p.5-20練習

new Something會建構一個新物件,由於()沒放值

會呼叫 Something() {  ... } , this(10)  ==  Something(int x) == Something (10)

所以會執行 x = 7 ,  println(x)

最後輸出結果為7;

arrow
arrow
    全站熱搜

    Justin258 發表在 痞客邦 留言(0) 人氣()